Problem Statement:

You are tasked with designing a system that adheres to the Liskov Substitution Principle (LSP) from the SOLID principles. The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. The goal is to create a system where derived classes can be used interchangeably with their base class without causing issues.

Solution:

  1. Define a Base Class:

    public class Bird {
        public void fly() {
            System.out.println("The bird is flying.");
        }
    }
  2. Create Derived Classes:

    public class Sparrow extends Bird {
        // Additional methods or overrides can be added
    }
    public class Penguin extends Bird {
        @Override
        public void fly() {
            System.out.println("Penguins cannot fly.");
        }
    }
  3. Example Usage:

    public class Main {
        public static void main(String[] args) {
            Bird sparrow = new Sparrow();
            sparrow.fly();  // Outputs: "The bird is flying."
    
            Bird penguin = new Penguin();
            penguin.fly();  // Outputs: "Penguins cannot fly."
        }
    }